package org.wso2.carbon.registry.custom.dia.ui.utils;

import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.wso2.carbon.registry.common.ui.UIException;
import org.wso2.carbon.registry.resource.ui.clients.CustomUIServiceClient;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.custom.dia.ui.clients.TopicServiceClient;

public class SaveDocumentUtil {

    private static final Log log = LogFactory.getLog(SaveDocumentUtil.class);
    private TopicServiceClient client; 

    public SaveDocumentUtil(ServletConfig config, HttpSession session
            ) throws UIException {
        try {
            client = new TopicServiceClient(config, session);
        } catch (RegistryException e) {
             String msg = "Failed to add documentation. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }

    public void saveEndpointBean(
            String path, HttpServletRequest request, ServletConfig config, HttpSession session) throws UIException {

        try {
            CustomUIServiceClient customUIServiceClient =
                    new CustomUIServiceClient(config, session);

            /*Structure of the OM
             *
             * <document>
                  <title>xxx</title>
                  <summary>xxxx</summary>
                  <externalLink>http://xxx</externalLink>
             * </document>
             */ 
 
            String title = request.getParameter("diaTitle");
            String summary = request.getParameter("diaSummary");
            String link = request.getParameter("diaLink"); 
 
            String product = request.getParameter("product");
            String productRelease = request.getParameter("productRelease");
            String carbonPlatform = request.getParameter("carbonPlatform");
            String jdkPlatform = request.getParameter("jdkPlatform");
                       
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMElement document = fac.createOMElement("document", null);
            OMElement titleElement = fac.createOMElement("title", null);
                titleElement.setText(title);
            OMElement summaryElement = fac.createOMElement("summary", null);
                summaryElement.setText(summary);
            OMElement linkElement = fac.createOMElement("externalLink", null);
                linkElement.setText(link);


            OMElement relatedProduct = fac.createOMElement("relatedProduct", null);
                OMElement productElement = fac.createOMElement("product", null);
                    productElement.setText(product);
                OMElement releaseElement = fac.createOMElement("release", null);
                    releaseElement.setText(productRelease);
            relatedProduct.addChild(productElement);
            relatedProduct.addChild(releaseElement);
            
            OMElement environment = fac.createOMElement("environment", null);
                OMElement carbonPlatformElement = fac.createOMElement("carbonPlatform", null);
                    carbonPlatformElement.setText(carbonPlatform);
                OMElement jdkElement = fac.createOMElement("jdk", null);
                    jdkElement.setText(jdkPlatform);
            environment.addChild(carbonPlatformElement);
            environment.addChild(jdkElement);
            
            document.addChild(titleElement);
            document.addChild(summaryElement);
            document.addChild(linkElement);
            document.addChild(relatedProduct);
            document.addChild(environment);

            String content = document.toString();
            customUIServiceClient.updateTextContent(path, content);
        } catch (Exception e) {
            String msg = "Failed while saving the documentation. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
    public void saveAssociations(
           String path, HttpServletRequest request, ServletConfig config, HttpSession session) throws UIException {
    
        int numOfAsso = Integer.parseInt(request.getParameter("numOfAsso"));

        for(int i=0;i<=numOfAsso;i++){
            String assoType = "related-document";//request.getParameter("aType"+i);
            String assoPath = request.getParameter("aPath"+i);
            client.addAssociation(path, assoPath, assoType);
        }
}

    public void saveTags(
           String path, HttpServletRequest request, ServletConfig config, HttpSession session) throws UIException {

                try{ 
            String tags = request.getParameter("diaTags");
            Scanner sc = new Scanner(tags);
            sc.useDelimiter(",");

            String tag;
            while(sc.hasNext()){
                tag = sc.next();
                tag.trim();
                client.applyTag(path, tag);
            }
        }catch(Exception e){
            String msg = "Failed while saving tags. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
}

